package com.example.sourcecodelearning.modules.designpattern.responsibilitychain.handler.impl;

import com.example.sourcecodelearning.modules.designpattern.responsibilitychain.Request;
import com.example.sourcecodelearning.modules.designpattern.responsibilitychain.handler.RequestHandler;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Objects;

/**
 * @author yuzhenxin
 * @date 2023/8/1
 */
@Service
public class CheckRequestHandler implements RequestHandler {

    @Override
    public void handleRequest(Request request) {
        if (CollectionUtils.isEmpty(request.getHeaders())) {
            throw new RuntimeException("no authorization header");
        }
        String header = request.getHeader(HttpHeaders.AUTHORIZATION);
        if (!Objects.equals(header, "token")) {
            throw new RuntimeException("error token");
        }
        System.out.println("check request:" + request);
    }

}
